package form

import (
	"github.com/go-playground/validator/v10"
	"server/component/errorx"
)

type ValidatorMessages map[string]string

type Validator interface {
	// 获取验证器自定义错误信息
	GetMessage() ValidatorMessages
}

func GetErrorMsg(request Validator, err error) error {
	errMsg := "Parameter error"
	errors, ok := err.(validator.ValidationErrors)
	if ok {
		for _, v := range errors {

			if message, exist := request.GetMessage()[v.Field()+"."+v.Tag()]; exist {
				errMsg += message
			} else {
				errMsg += v.Error()
			}
		}
	}
	return errorx.New(400, errMsg)
}
